<?php

namespace App\Admin\Controllers;

use App\Models\GotoList;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class GotosController extends Controller
{
    use ModelForm;

    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('广告列表');
            $content->body($this->grid());
        });
    }
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header('编辑广告');
            $content->body($this->form()->edit($id));
        });
    }
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header('创建广告');
            $content->body($this->form());
        });
    }
    protected function grid()
    {
        return Admin::grid(GotoList::class, function (Grid $grid) {
            $grid->id('ID')->sortable();
            $grid->title('广告标题');
            $grid->name('广告名称');
            $grid->column('position','position')->display(function ($type){
                switch ($type)
                {
                    case 0:
                        return '首页文字';
                        break;
                    case 1:
                        return '内页右侧图片';
                        break;
                    case 2:
                        return '首页banner_1';
                        break;
                    case 3:
                        return '首页banner_2';
                        break;
                    case 4:
                        return '首页banner_3';
                        break;
                    case 5:
                        return '首页banner_4';
                        break;
                    case 6:
                        return '秀目录banner_1';
                        break;
                    case 7:
                        return '秀目录banner_2';
                        break;
                    case 8:
                        return '秀资讯banner_1';
                        break;
                }
            });
            $grid->actions(function ($actions) {
                $actions->disableDelete();
            });
            $grid->tools(function ($tools) {
                // 禁用批量删除按钮
                $tools->batch(function ($batch) {
                    $batch->disableDelete();
                });
            });
        });
    }
    protected function form()
    {
        return Admin::form(GotoList::class, function (Form $form) {
            $form->display('id', 'ID');
            // 创建一个输入框，第一个参数 title 是模型的字段名，第二个参数是该字段描述
            $form->text('name', '名称')->rules('required');// 创建一个输入框，第一个参数 title 是模型的字段名，第二个参数是该字段描述
            $form->text('title', '标题')->rules('required');
            $form->image('img', '图片')->rules('required');
            $form->url('url', '地址')->rules('required');
            $form->select('type', '类型')->options([0 => '文字', 1 => '图片'])->rules('required');
            $form->select('position', '位置')->options([
                0 => '首页文字',
                1 => '内页右侧图片',
                2 => '首页banner_1',
                3 => '首页banner_2',
                4 => '首页banner_3',
                5 => '首页banner_4',
                6 => '秀目录banner_1',
                7 => '秀目录banner_2',
                8 => '秀资讯banner_1',
                9 => 'pages_banner_1',
                10 => '秀标签_banner_1',
            ])->rules('required');
            $form->date('expire_time','到期时间')->format('YYYY-MM-DD')->placeholder('请输入到期时间')->rules('required',[
                'required' => '请填写到期时间',
            ]);
            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }
}
